#include "frmmain.h"
#include "ui_frmmain.h"

frmMain::frmMain(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::frmMain)
{
    ui->setupUi(this);
    init();
    QRegExp priceExp("^(-?[0]|-?[1-9][0-9]{0,5})(?:\\.\\d{1,4})?$|(^\\t?$)");
    QRegExpValidator *pRegExpValidator2 = new QRegExpValidator(priceExp,this);
    ui->txtaPrice->setValidator(pRegExpValidator2);
}

frmMain::~frmMain()
{
    delete ui;
}

void frmMain::getData()
{
    QByteArray datagram=client->readAll();
    QString str(datagram);
    qDebug()<<str;
    QStringList temp=str.split('|');
    QStringList t=temp;
    qDebug()<<"1";
    t.removeFirst();
    if(temp[0]=="6100")
    {
        ui->listWidget->clear();
        ui->listWidget->addItems(t);
    }
    else if(temp[0]=="6200")
    {
        ui->txtName->setText(temp[1]);
        ui->txtAuthor->setText(temp[2]);
        ui->txtType->setText(temp[3]);
        ui->txtPrice->setText(temp[4]);
        ui->txtNote->setText(temp[5]);
        ui->txtState->setText(temp[6]);
    }
    else if(temp[0]=="6300")
    {
        if(temp[1]=="1")
            {
                ui->txtaName->setText("");
                ui->txtaAuthor->setText("");
                ui->txtaType->setText("");
                ui->txtaPrice->setText("");
                ui->txtaNote->setText("");
                ui->txtaState->setText("");
                QMessageBox::information(this,"info","Add success!");

            }
        else
            QMessageBox::warning(this,"warning","Add failed!");

    }
    else if(temp[0]=="6400")
    {
        if(temp[1]=="1")
            {
                ui->txtName->setText("");
                ui->txtAuthor->setText("");
                ui->txtType->setText("");
                ui->txtPrice->setText("");
                ui->txtNote->setText("");
                ui->txtState->setText("");
                QMessageBox::information(this,"info","Delete success!");
            }
        else
            QMessageBox::warning(this,"warning","Delete failed!");

    }
    else
    {
        QMessageBox::warning(this,"warning","get book info error!");
    }
    disconnect(client,SIGNAL(readyRead()),this,SLOT(getData()));
    client->close();
    if(temp[0]=="6300"||temp[0]=="6400")         //reflash when add book or delete book
        init();
}

void frmMain::init()
{
    config=new Config();
    client=new QTcpSocket();
    server=config->getServer();
    port=config->getPort();
    username=config->getUsername();
    client->connectToHost(QHostAddress(server),port);
    if(!client->waitForConnected(5000))
    {
        QMessageBox::warning(this,"warning","Connection Error!");
        close();
        return;
    }
    QString str="6101|"+username;
    //qDebug()<<str;
    QByteArray qb=str.toLatin1();
    char* s=qb.data();
    client->write(s);
    connect(client,SIGNAL(readyRead()),this,SLOT(getInit()));
}

void frmMain::on_listWidget_itemClicked(QListWidgetItem *item)
{
    ui->tabWidget->setCurrentIndex(0);
    bookname=item->text();
    client->connectToHost(QHostAddress(server),port);
    if(!client->waitForConnected(5000))
    {
        QMessageBox::warning(this,"warning","Connection Error!");
        close();
        return;
    }
    QString str="6201|"+username+"|"+bookname;
    //qDebug()<<str;
    QByteArray qb=str.toLatin1();
    char* s=qb.data();
    client->write(s);
    connect(client,SIGNAL(readyRead()),this,SLOT(getData()));
}

void frmMain::on_btnAddbook_clicked()
{
    if(ui->txtaName->text()==""||ui->txtaAuthor->text()==""||
            ui->txtaType->text()==""||ui->txtaPrice->text()==""||
            ui->txtaNote->text()==""||ui->txtaState->text()=="")
    {
        QMessageBox::warning(this,"warning","Input error!");
        return;
    }
    QString bname=ui->txtaName->text();
    QString bauthor=ui->txtaAuthor->text();
    QString btype=ui->txtaType->text();
    QString bprice=ui->txtaPrice->text();
    QString bnote=ui->txtaNote->text();
    QString bstate=ui->txtaState->text();
    client->connectToHost(QHostAddress(server),port);
    if(!client->waitForConnected(5000))
    {
        QMessageBox::warning(this,"warning","Connection Error!");
        close();
        return;
    }
    QString str="6301|"+username+"|"+bname+"|"+bauthor+"|"+btype+"|"+bprice+"|"+bnote+"|"+bstate;
    qDebug()<<str;
    QByteArray qb=str.toLatin1();
    char* s=qb.data();
    client->write(s);
    connect(client,SIGNAL(readyRead()),this,SLOT(getData()));
}

void frmMain::getInit()
{
    QByteArray datagram=client->readAll();
    QString str(datagram);
    qDebug()<<str;
    QStringList temp=str.split('|');
    QStringList t=temp;
    qDebug()<<"1";
    t.removeFirst();
    if(temp[0]=="6100")
    {
        ui->listWidget->clear();
        ui->listWidget->addItems(t);
    }
    else
    {
        QMessageBox::warning(this,"warning","get book info error!");
    }
    disconnect(client,SIGNAL(readyRead()),this,SLOT(getInit()));
    client->close();
}

void frmMain::on_btnDelete_clicked()
{
    if(bookname=="")
    {
        QMessageBox::warning(this,"warning","no book is selected!");
        return;
    }
    client->connectToHost(QHostAddress(server),port);
    if(!client->waitForConnected(5000))
    {
        QMessageBox::warning(this,"warning","Connection Error!");
        close();
        return;
    }
    QString str="6401|"+username+"|"+bookname;
    //qDebug()<<str;
    QByteArray qb=str.toLatin1();
    char* s=qb.data();
    client->write(s);
    connect(client,SIGNAL(readyRead()),this,SLOT(getData()));
}

